home *** CD-ROM | disk | FTP | other *** search
- Path: news.sprintlink.net!datalytics!usenet
- From: Rob Stewart <stew@datalytics.com>
- Newsgroups: comp.programming,comp.lang.c++
- Subject: Re: C++ problem with objects and pointers (I think).
- Date: Fri, 05 Apr 1996 18:05:34 -0500
- Organization: Datalytics, Inc
- Message-ID: <3165A73E.F98@datalytics.com>
- References: <3161F52E.3561@abc.se>
- NNTP-Posting-Host: 204.62.224.71
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Kjell Franzen wrote:
- >
- > Using a debugger shows me that putting the values there works fine, but
- > when i want to get them and print to screen I get totally out of the
- > blue (out of the Ram:)) values. Do my program "forget" the adresses, is
- > my pointer initiation wrong? HELP!
-
- First, let me point out that C++ questions like this should be
- directed to comp.lang.c++ or c.l.c++.moderated in the future.
- However, you'll be looking for the answer in comp.programming,
- so I'll reply there.
-
- ---------------------------------------------------------------
- [snip]
- > class Object
- > {
- > private:
- > point *vertices;
- > facet *polygons;
- > int n_vertices;
- > int n_polygons;
- > public:
- > Object(void);
- > void init_object(void);
- > void print_object(void);
- > };
- > Object::Object(void)
- > {
- > //here I zero out all variables, structs and pointers
- > };
-
- ^ not needed here
-
- You should implement the ctor with an initializer list; it's a
- good habit to develop:
-
- Object::Object(void):
- vertices(0),
- polygons(0),
- n_vertices(0),
- n_polygons(0)
- {
- }
-
- [snip]
- > void Object::init_object(void)
- > {
- > n_vertices = 3;
- > n_polygons = 1;
- > struct point *vertices = new struct point[n_vertices];
- > struct facet *polygons = new struct facet[n_polygons];
-
- There is a problem with the lines above. You have declared two
- new pointers here. These have scope throughout the function
- from the point of declaration forward. (Also, the keyword
- struct is not strictly necessary in C++.) Try this:
-
- vertices = new point[n_vertices];
- polygons = new facet[n_polygons];
-
- [snip]
- > void Object::print_object(void)
- > {
- > cout << "n_vertices: " << n_vertices << endl;
- > cout << "n_polygons: " << n_polygons << endl;
- > for (int i = 0 ; i < n_vertices ; i++)
- > {
- > cout << "vertices[" << i << "].x: " << vertices[i].x << endl;
- > cout << "vertices[" << i << "].y: " << vertices[i].y << endl;
- > cout << "vertices[" << i << "].z: " << vertices[i].z << endl;
- > };
-
- ^ not needed here
-
- > for (i = 0 ; i < n_polygons ; i++)
- > for (int j = 0 ; j < 3 ; j++)
- > cout<<"polygons["<< i <<"].vtx["<< j <<"]: "<<polygons[i].vtx[j]<<"\n";
- > }
-
- You can combine these lines and improve output performance like
- this:
-
- inline // or not; your call
- void DisplayVertex(
- const int i_Index,
- const char i_Vertex,
- const long i_Value)
- {
- cout << "\nvertices[" << i_Index << "]." << i_Vertex << ": "
- i_Value;
- }
-
- void Object::print_object(void)
- {
- cout << "n_vertices: " << n_vertices
- << "\nn_polygons: " << n_polygons;
- for (int i = 0; i < n_vertices; i++)
- {
- DisplayVertex(
- i,
- 'x',
- vertices[i].x);
- DisplayVertex(
- i,
- 'y',
- vertices[i].y);
- DisplayVertex(
- i,
- 'z',
- vertices[i].z);
- }
- for (i = 0; i < n_polygons; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- cout << "\npolygons[" << i << "].vtx[" << j << "]: "
- << polygons[i].vtx[j];
- }
- }
- cout << endl;
- }
-
- The differences are several. First, the inline function avoids
- code duplication without changing the end result. You can
- change the inline function once and affect all three uses of it.
- (Whether to make inline or not is a space-time tradeoff you'll
- have to make.)
-
- Second, note that I am using newlines rather than calling the
- endl manipulator in all but the last line. The difference can
- be pronounced. You see, endl not only inserts a newline, it
- also calls the flush mf. Thus, your implementation wasn't
- allowing much use of the output buffer in cout.
-
- Finally, note that you don't have to reference cout as many
- times as you did. There is no penalty in doing so, aside from
- the lost use of a temporary each time. Besides, an optimizing
- compiler will probably not generate those unused temporaries.
-
- --
- Robert Stewart | My opinions are usually my own.
- Datalytics, Inc. | stew@datalytics.com
-